package org.kivo.kipooo.utils;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.persistence.PersistentDataAdapterContext;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.kivo.kipooo.Kipooo;

@SuppressWarnings("ALL")
public class LocationData implements PersistentDataType<PersistentDataContainer , Location> {
    public static final LocationData INSTANCE = new LocationData();

    @SuppressWarnings("NullableProblems")
    @Override
    public Class<PersistentDataContainer> getPrimitiveType() {
        return PersistentDataContainer.class;
    }

    @Override
    public Class<Location> getComplexType() {
        return Location.class;
    }

    @Override
    public PersistentDataContainer toPrimitive(Location complex, PersistentDataAdapterContext context) {
        PersistentDataContainer container = context.newPersistentDataContainer();
        container.set(new NamespacedKey(Kipooo.INSTANCE , "homeLocX") , PersistentDataType.DOUBLE , complex.getX());
        container.set(new NamespacedKey(Kipooo.INSTANCE , "homeLocY") , PersistentDataType.DOUBLE , complex.getY());
        container.set(new NamespacedKey(Kipooo.INSTANCE , "homeLocZ") , PersistentDataType.DOUBLE , complex.getZ());
        container.set(new NamespacedKey(Kipooo.INSTANCE , "homeWorldName") , PersistentDataType.STRING , complex.getWorld().getName());
        return container;
    }

    @Override
    public Location fromPrimitive(PersistentDataContainer primitive, PersistentDataAdapterContext context) {
        Location location = new Location(Bukkit.getWorld(primitive.get(new NamespacedKey(Kipooo.INSTANCE , "homeWorldName"), PersistentDataType.STRING)) ,
                primitive.get(new NamespacedKey(Kipooo.INSTANCE , "homeLocX") , PersistentDataType.DOUBLE) ,
                primitive.get(new NamespacedKey(Kipooo.INSTANCE , "homeLocY") , PersistentDataType.DOUBLE) ,
                primitive.get(new NamespacedKey(Kipooo.INSTANCE , "homeLocZ") , PersistentDataType.DOUBLE));
        return location;
    }

}